package com.example.mall_image_server.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * Redis工具类
 */
@Component
public class RedisUtils {
    private static RedisTemplate redisTemplate;   //key-value是对象的

    @Autowired
    public void setRedisTemplate(@Qualifier("redisTemplate") RedisTemplate template) {
        RedisUtils.redisTemplate = template;
    }

    /**
     * 判断是否存在key
     * @param key
     * @return
     */
    public static boolean hasKey(String key){
        return redisTemplate.hasKey(key);
    }

    /**
     * 获取值
     * @param key
     * @return
     */
    public static Object get(String key){
        return  redisTemplate.opsForValue().get(key);
    }

    /**
     * 插入值
     * @param key
     * @param value
     * @return
     */
    public static boolean set(final String key,Object value){
        boolean result = false;
        try{
            redisTemplate.opsForValue().set(key,value);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  result;
    }
}